<?php namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\Tag;
use think\Controller;
//标签控制器
class Tags extends AdminBase{
	private $db;
	public function _initialize(){
		$this->db=new Tag();
	
	}
	
	//栏目列表
	public function index(){
		$data=db('tag')->order('sort asc')->select();
		$this->assign('data',$data);
		return $this->fetch();
	}
	//添加栏目
	public function add(){
		if(request()->isPost()){
			$data=input('post.');
			
			$res=$this->db->addData(input('post.'));
			if($res['valid']){
				$this->success($res['msg'],'tags/index');exit;
				
			}else{
				$this->error($res['msg']);exit;
			}
			
		}else{
			
			return $this->fetch();
		}
		
		
	}
	
	//栏目编辑
	public function edit(){
		if(request()->isPost()){
		
			$res=$this->db->editData(input('post.'));
//			halt(input('post.'));
			if($res['valid']){
				
				$this->success($res['msg'],'index');exit;
				
			}else{
				
				$this->error($res['msg']);exit;
			}
		}else{
			
			$tid=input('tid');
//			halt($tid);
			//获得对应的栏目数据
			$oneData=db('tag')->where('tid',$tid)->find();
//			halt($oneData);
			$this->assign('oneData',$oneData);
//			
	    	return $this->fetch();
			
		}
	
	}
	//删除
	public function delete(){
		$tid=input('tid');
//		halt($tid);
	   $num=db('tag')->delete($tid);
//	    echo count($cids);exit;
	    if($num){
	    
	    	$this->success('标签删除成功！');
	    }else{
	    	$this->error('标签删除失败！');
	    }
	}
	//排序
	public function sort(){
		$sorts=input('post.');
//	   var_dump($sorts);
		foreach($sorts as $k=>$v){
			if(empty($v)||$v>100||$v<1){
				$this->error('标签排序只能在1-100之间');
			}else{
				db('tag')
				    ->where('tid',$k)
				    ->setField('sort',$v);
			}
        }
	   $this->success('标签排序成功！');
	}
	
}

